#include <iostream>
using namespace std;
#include <QProcess>
#include <QFile>
#include <QTextStream>
#include <QMenu>
#include "dialog.h"
MyDlg::MyDlg()
{
    ui.setupUi(this);
	conn();
	init();
} 

void MyDlg::init()
{
	quitAction = new QAction(tr("&Quit"), this);
	QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));

	trayIconMenu = new QMenu(this);
	trayIconMenu->addAction(quitAction);

	trayIcon = new QSystemTrayIcon;
	trayIcon->setContextMenu(trayIconMenu);
	trayIcon->setIcon(QIcon(TRAY_ICON_PATH));
	trayIcon->show();
	trayIcon->setToolTip("QT IPTV");
	QObject::connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 
			this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}

void MyDlg::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
	switch(reason) 
	{
	case QSystemTrayIcon::Trigger:
		show();
		break;
	default:
		;
	}
}


void MyDlg::closeEvent(QCloseEvent *event)
{
    if(!sender() && isVisible()) 
	{
        hide();
        event->ignore();
    }
}

void MyDlg::ExecMplayer()
{
	QPushButton *pushSendButton = (QPushButton *)sender();
	QString name;
	QString addr;
	name = pushSendButton->text();
	addr = NameToAddr(name);
//	cout<<qPrintable(name)<<endl;
//	cout<<qPrintable(addr)<<endl;
	QStringList args;
	args<<addr;
	QProcess* mplayerProcess=new QProcess;
	mplayerProcess->start("mplayer",args);
}

QString MyDlg::NameToAddr(QString name)
{
	QString addr;
	QStringList allstring;
    QFile file(TVLIST_PATH);
    if(file.open(QIODevice::ReadOnly))
	{
        QTextStream stream(&file);
        QString string;
        while(!stream.atEnd())
		{
			stream>>string;
            allstring += string;
        }
        file.close();
    }
	int index = allstring.indexOf(name);
//	cout<<allstring.indexOf(name)<<endl;
	addr = allstring.at(index-1);
	return addr;
}

void MyDlg::conn()
{

    QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_7, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_8, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_9, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_10, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_11, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_12, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_13, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_14, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_15, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_16, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_17, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_18, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_19, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_20, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_21, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_22, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_23, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_24, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_25, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_26, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_27, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_28, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_29, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_30, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_31, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_32, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_33, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_34, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_35, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_36, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_37, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
    QObject::connect(ui.pushButton_38, SIGNAL(clicked()), this, SLOT(ExecMplayer()));
}
